package com.ouc.tcp.test;

import java.util.Arrays;
import java.util.zip.CRC32;

import com.ouc.tcp.message.TCP_HEADER;
import com.ouc.tcp.message.TCP_PACKET;

public class CheckSum {
	
	/*计算TCP报文段校验和：校验TCP首部中的seq、ack和sum，以及TCP数据字段*/
	public static short computeChkSum(TCP_PACKET tcpPack) {
		//使用CRC校验，参与计算的有Sequence Number (seq)、Acknowledgment Number (ack)、TCP数据字段
		CRC32 checksum = new CRC32();
		checksum.update(tcpPack.getTcpH().getTh_seq());
		checksum.update(tcpPack.getTcpH().getTh_ack());
		int[] data=tcpPack.getTcpS().getData();
		for(int data_no : data) {
			checksum.update(data_no);
		}
		checksum.update(Arrays.toString(tcpPack.getTcpS().getData()).getBytes());
		return (short) checksum.getValue();
	}
	
}
